// pages/order/order.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    unread:1,
    tabList:['接单池','待接单','进行中','已完成','售后订单'],
    orderState:0,
    list:[],
    finish: false,
    loading: false,
    page: 1,
    active: 0,
    token: wx.getStorageSync('token')
  },
  orderBtnTap(e){
    wx.requestSubscribeMessage({
      tmplIds: [app.globalData.templateconfig.skill_order_template,app.globalData.templateconfig.skill_sales_template],
      success :(res) =>{ 
        console.log(res)
      },
      fail :(e) =>{ 
        console.log(e)
      },
      complete :(res) =>{ 
        if(e.detail.type == 1){
     
          http.get('order/skillaccept',{
            id:  e.detail.info.id
          }, false, false).then(res => {
            util.toast('抢单成功')
            this.reload()
            setTimeout(res => {
              util.skip('/pages/orderDetail/orderDetail?id='+e.detail.info.id)
            },1000)
          }).catch(err => {
            let data = err.data
            if(data.code === 2){
              util.toast(data.msg)
            }else if(data.code === 0){
              util.toast(data.data)
            }
          })
        }else if(e.detail.type == 2){
          http.get('order/skillaccept',{
            id:  e.detail.info.id
          }, false, false).then(res => {
            util.toast('接单成功')
            this.reload()
            setTimeout(res => {
              util.skip('/pages/orderDetail/orderDetail?id='+e.detail.info.id)
            },1000)
          }).catch(err => {
            let data = err.data
            if(data.code === 2){
              
              if(e.detail.info.shop_id) {
                util.toast(data.msg)
              }else{
                util.toast(data.msg)
                setTimeout(()=>{
                  util.skip('/service/earnestMoney/earnestMoney')
                },1000)
              }
              
            }else if(data.code === 0){
              util.toast(data.data)
            }
          })
        }
      }
    })
    
  },
  orderTap(e){
    util.skip('/pages/orderDetail/orderDetail?id='+e.detail.id)
  },
  onChange(event) {
    this.setData({
      active: event.detail.index
    })
    this.reload()
  },
  reload(){
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more(){
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList(){
    if(!wx.getStorageSync('token')) return
    if(this.data.finish){
      return 
    }
    if(this.data.loading){
      return 
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
    }
    if(this.data.active === 0){
      data['is_pool'] = 1
    }else if(this.data.active === 1){
      data['status'] = 1
    }else if(this.data.active === 2){
      data['not_finish'] = 1
    }else if(this.data.active === 3){
      data['finish'] = 1
    }else if(this.data.active === 4){
      data['in_service'] = 1
    }
    http.get('order/skillorderlist', data).then(res => {
      if(res.data.length === 0){
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data)
      this.setData({
        list: arr
      })
      
    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      token: wx.getStorageSync('token')
    })
    this.reload()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})